<?php
require_once 'util/class.dbservice.php';

class NotifyDao {
	
	 public function getNotify($userId) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return null;
	 		
	 		return $collection->findOne(array('_id'=>$userId));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "NotifyDao.getNotify error, reason=".$e);
	 		return null;
	 	}
	 }
	 
	 
	 public function addNotify($userId, $values) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return false;
	 		
	 		
	 		return $collection->update(array('_id'=>$userId),
	 					array('$inc'=>$values),
	 					array('upsert'=>true));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "NotifyDao.addNotify error, reason=".$e);
	 		return false;
	 	}
	 }
	 
	 public function removeNotify($userId, $values) {
	 	try {
	 		$collection = $this->getCollection();
	 		if(!$collection)
	 			return false;
	 	
	 		$values['lastReadTime']=time();
	 		return $collection->update(array('_id'=>$userId),
	 				array('$set'=>$values),
	 				array('upsert'=>true));
	 	} catch(Exception $e) {
	 		syslog(LOG_ERR, "NotifyDao.removeNotify error, reason=".$e);
	 		return false;
	 	}
	 }
	 
	 private function getCollection() {
	 	$db=DBService::getGlobalDb();
	 	return $db->notify;
	 }
}